using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using CS4730_XNA_Graphics_Tutorial.Lights;

namespace CS4730_XNA_Graphics_Tutorial {
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Scene scene;
        List<ModelRenderable> crates;

        Camera camera;

        public Game1() {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize() {
            base.Initialize();

            camera = new Camera(graphics.GraphicsDevice.DisplayMode.AspectRatio, 90.0f);
            camera.view = Matrix.CreateLookAt(Vector3.Backward * 200 + Vector3.Right * 100, new Vector3(-50, 50, 50), Vector3.Up);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent() {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            scene = new Scene(GraphicsDevice, Content);
            Model crateModel = Content.Load<Model>("Assets/Models/Crate/crate");
            Texture2D crateTexture = Content.Load<Texture2D>("Assets/Models/Crate/crate_tex");
            crates = new List<ModelRenderable>();
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    for (int k = 0; k < 5; k++) {
                        ModelRenderable crate = new ModelRenderable(crateModel, crateTexture, graphics.GraphicsDevice);
                        crate.transform = Matrix.CreateTranslation(
                            (float)(i * 250 - 250 * 2.5),
                            (float)(j * 250 - 250 * 2.5),
                            (float)(k * 250 - 1000));
                        crates.Add(crate);
                        scene.renderables.Add(crate);
                    }
                }
            }

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent() {

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime) {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            float millis = (float) gameTime.ElapsedGameTime.TotalMilliseconds;
            // update the camera
            camera.view *= Matrix.CreateRotationZ(0.001f * millis);

            // update the crates
            foreach (ModelRenderable crate in crates)
            {
                crate.transform = Matrix.CreateRotationX(0.001f * millis) * crate.transform;
                crate.transform = Matrix.CreateRotationY(0.0003f *millis) * crate.transform;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            scene.render(gameTime, camera);

            base.Draw(gameTime);
        }
    }
}
